home *** CD-ROM | disk | FTP | other *** search
/ IRIX Installation Tools & Overlays 2002 November / SGI IRIX Installation Tools & Overlays 2002 November - Disc 4.iso / dist / infosearch.idb / usr / lib / infosearch / bin / namazu_sgi.z / namazu_sgi
Text File  |  2002-10-15  |  5KB  |  232 lines

  1. #!/usr/bin/perl
  2. #
  3. # Copyright 1996-2002, Silicon Graphics, Inc.
  4. # All Rights Reserved.
  5. #
  6. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  7. # the contents of this file may not be disclosed to third parties, copied or
  8. # duplicated in any form, in whole or in part, without the prior written
  9. # permission of Silicon Graphics, Inc.
  10. #
  11. # RESTRICTED RIGHTS LEGEND:
  12. # Use, duplication or disclosure by the Government is subject to restrictions
  13. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  14. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  15. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  16. # rights reserved under the Copyright Laws of the United States.
  17. #
  18. #  ----------
  19. #  namazu_sgi
  20. #  ----------
  21. #  L10N search wrapper
  22. #
  23. #  Usage:
  24. #       namazu_sgi [-p <db_pth>] <query>
  25. #
  26. #  requires perl5
  27. #
  28.  
  29. $| = 1;
  30.  
  31. use strict;
  32.  
  33. # globals
  34. #
  35. package InfoSearch;
  36.  
  37. $InfoSearch::COLLECTION = $ENV{'COLLECTION'};
  38. $InfoSearch::_WEB       = ($ENV{'REQUEST_METHOD'} ne '' ? 1 : 0);
  39. $InfoSearch::_DB        = 'bks';
  40. %InfoSearch::_bk_ttls   = ();
  41.  
  42. &main(@ARGV);
  43. exit(0);
  44.  
  45.  
  46.  
  47. #######################################################################
  48. #
  49. # void main()
  50. #
  51. #######################################################################
  52.  
  53. sub main {
  54.  
  55.   my(@argv) = @_;
  56.   unless ($argv[0]) {
  57.         print "namazu_sgi: no arguments\n" if (${InfoSearch::_WEB} == 0);
  58.         return;
  59.   }
  60.   if (!(-x "./namazu")) {
  61.         print "namazu_sgi: no program\n" if (${InfoSearch::_WEB} == 0);
  62.         return;
  63.   }
  64.  
  65.   # pth  = /usr/share/Insight/library/SGI_bookshelves
  66.   #
  67.   my($db_pth, $bk, $ttl, $hits, $srch_str, $cmd, $lang) = '';
  68.   my($i) = 0;
  69.   while($i < (@argv + 0)) {
  70.  
  71.         if ($argv[$i] eq "-p") { 
  72.  
  73.            splice(@argv, $i, 1);
  74.            if( $argv[$i] ne '' ) {
  75.                $db_pth = $argv[$i];
  76.                splice(@argv, $i, 1);
  77.            }
  78.  
  79.         } elsif ($argv[$i] eq "-db") {
  80.  
  81.            splice(@argv, $i, 1);
  82.            if( $argv[$i] ne '' ) {
  83.                $InfoSearch::_DB = $argv[$i];
  84.                splice(@argv, $i, 1);
  85.            }
  86.  
  87.         } elsif ($argv[$i] eq "-lang") {
  88.  
  89.            splice(@argv, $i, 1);
  90.            if( $argv[$i] ne '' ) {
  91.                $lang = $argv[$i];
  92.                splice(@argv, $i, 1);
  93.            }
  94.  
  95.         } else {
  96.            $i++;
  97.         }
  98.   }
  99.  
  100.   # take whatever is left
  101.   #
  102.   $srch_str = join(' ', @argv);
  103.  
  104.  
  105.   #---------------------------------------------------------
  106.   # set the proper locale for us to work in...
  107.   #
  108.   # LC_CTYPE   needed for uc{first}, lc{first}
  109.   # LC_COLLATE needed for lt, le, cmp, ge, gt, strcoll, sort
  110.   #
  111.   use locale;
  112.   use POSIX qw(locale_h);
  113.   use POSIX qw(strcoll);
  114.  
  115.   if( $lang ne '' ) {
  116.       if (!(setlocale(LC_CTYPE, $lang))) {
  117.  
  118.           # on error, set to 'C'
  119.           #
  120.           $lang = 'C';
  121.           setlocale(LC_CTYPE, $lang);
  122.       }
  123.       setlocale(LC_COLLATE, $lang);
  124.   }
  125.  
  126.  
  127.   # Set the path for security and taint checking.
  128.   #
  129.   $ENV{'PATH'} = "${InfoSearch::_TR}/usr/sbin:" .
  130.                  "${InfoSearch::_TR}/usr/bin:"  .
  131.                  "${InfoSearch::_TR}/bin:/usr/sbin:/usr/bin:/bin";
  132.  
  133.   # check
  134.   #
  135.   my($OK_CHARS) = "-a-zA-Z0-9_.*:;\+\/";
  136.   my($BAD_CHARS) = "`'\"&<>,;";
  137.   $db_pth =~ s/[^$OK_CHARS]//go;
  138.   $srch_str =~ s/[$BAD_CHARS]//go;
  139.  
  140.   # default path
  141.   #
  142.   if( $db_pth eq '' ) {
  143.       $db_pth = '/usr/share/Insight/library/SGI_bookshelves/SGIindex';
  144.   }
  145.   $cmd = "./namazu -a --result=sgi -h '${srch_str}' ${db_pth}";
  146.   $db_pth =~ s/\/SGIindex$//;
  147.  
  148.   &get_booklist($db_pth);
  149.  
  150.   $i = 0;
  151.   open(FP, "${cmd} |") || do {
  152.     print "namazu_sgi: cmd failed ($cmd) : $!\n";
  153.     return;
  154.   };
  155.  
  156.  
  157.   # parse up the result
  158.   #
  159.   while(<FP>) {
  160.  
  161.     if($i == 0) {
  162.        if($_ =~ /<!-- HIT -->/i) {
  163.           ($hits) = ($_ =~ m#\-\->([\d]+)<!\-\-#is);
  164.           $i = 1;
  165.           print "\n<P><B><font face=\"helvetica\" color=\"darkred\">",
  166.                 "  ${hits} document(s) found</font></B></P>\n\n";
  167.        }
  168.        next;
  169.     }
  170.  
  171.     if( $_ =~ s/$db_pth//g ) {
  172.         $bk = '';
  173.         if( $_ =~ /\/books\/([^\/]+)\// ) {
  174.             $bk = $1;
  175.         }
  176.     }
  177.  
  178.     if($_ =~ /<dl>/i) {
  179.        $_ =~ s/<dl>/<TABLE CELLPADDING="1" CELLSPACING="6" BORDER="0">/;
  180.     }
  181.     if($_ =~ /<\/dl>/i) {
  182.        print "</TABLE>\n";
  183.        last;
  184.     }
  185.        
  186.     if($_ =~ /XXDBXX/) {
  187.        $_ =~ s/XXDBXX/${InfoSearch::_DB}/g;
  188.        $_ =~ s/XXCOLLXX/${InfoSearch::COLLECTION}/g;
  189.     }
  190.     if($_ =~ /XXTTLXX/) {
  191.        $ttl = ${InfoSearch::_bk_ttls}{"$bk"};
  192.        $_ =~ s/XXTTLXX/$ttl/;
  193.     }
  194.  
  195.     print $_;
  196.   }
  197.   close(FP);
  198.  
  199.   if(length($hits) == 0) {
  200.      print "\n<P><B><font face=\"helvetica\" color=\"darkred\">",
  201.            "  ** No documents found</font></B></P>\n\n";
  202.   }
  203.  
  204.   return;
  205. }
  206.  
  207.  
  208. sub get_booklist {
  209.  
  210.   my($base) = @_;
  211.   my($bk, $ttl) = '';
  212.  
  213.   open (FP, "cat $base/*/booklist.txt |") || return;
  214.   while(<FP>) {
  215.  
  216.       ($bk, $ttl) = '';
  217.       if( $_ =~ /NAME[\ \=]+\"([^\"]+)\"/i ) { 
  218.           $bk = $1;
  219.           if( $_ =~ /ALIAS[\ \=]+\"([^\"]+)\"/i ) {
  220.               $ttl = $1;
  221.           } else {
  222.               $ttl = $bk;
  223.           }
  224.  
  225.           ${InfoSearch::_bk_ttls}{"$bk"} = "$ttl";
  226.       }
  227.   }
  228.   close(FP);
  229. }
  230.  
  231.  
  232.